/*
 *  atBudget Personal Finance Management application
 *
 *  Copyright 2011, Ilia Frenkel <frenkel.ilia@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
	Payee model defines fields of the payee and proxy for server
	communication.
 */
Ext.define('ATB.model.Payee', {
    extend: 'Ext.data.Model',
    fields: [
		{name: 'id',          type: 'int'   },
		{name: 'name',        type: 'string'},
		{name: 'description', type: 'string'}
	],
	proxy: {
		type: 'rest',
		url: 'data/payees/index.php',
		reader: {
			type: 'json',
			root: 'payees',
			successProperty: 'success'
		},
		writer: {
			type: 'json',
			successProperty: 'success'
		},
		listeners: {
			exception: function(proxy, res, oper, opts) {
				switch (oper.action) {
					case 'read':
						if (oper.success == false) {
							Ext.Msg.alert('Error', 'Failed to get a list of '+
							'payees. Server returned an error.');
						}
						break;
					case 'create':
						if (oper.success == false) {
							var store = oper.records[0].store;
							store.remove(oper.records);
							Ext.Msg.alert('Error', 'Failed to create new payee.'+
							' Server returned an error.');
						}
						break;
					case 'update':
						if (oper.success == false) {
							oper.records[0].set(oper.records[0].raw);
							Ext.Msg.alert('Error', 'Failed to update payee. '+
							'Server returned an error.');
						}
						break;
					case 'destroy':
						if (oper.success == false) {
							Ext.Msg.alert('Error', 'Failed to delete payee. '+
							'Server returned an error.');
						}
						break;
					default:
						Ext.Msg.alert('Error', 'An unexpected error occured '+
						'while performing '+oper.action+' operation. Server '+
						'is saying: '+res.responseText);
				}//switch
			}//exception
		}//listeners
	}//proxy
});